import { ElMessage } from 'element-plus';
import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'


const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',

    component: () => import(/* webpackChunkName: "login" */ '../views/Login.vue')
  },
  {
    path: '/home',
    name: 'home',
    redirect: '/dity',
    component: () => import(/* webpackChunkName: "home" */ '../views/Home.vue'),
    children: [
      {
        path: '/dity',
        name: 'dity',
        component: () => import(/* webpackChunkName: "dity" */ '../views/CommoDity.vue'),
        meta: {
          title: '商品',
          icon: 'ShoppingTrolley'
        }
      },
      {
        path: '/ber',
        name: 'ber',
        component: () => import(/* webpackChunkName: "ber" */ '../views/Member.vue'),
        meta: {
          title: '会员',
          icon: 'User'
        }
      },

      {
        path: '/nxdata',
        name: 'nxdata',
        component: () => import(/* webpackChunkName: "nxdata" */ '../views/Nxdata.vue'),
        meta: {
          title: '数据',
          icon: 'Coin'
        },
      },
      {
        path: '/order',
        name: 'order',
        component: () => import(/* webpackChunkName: "order" */ '../views/Order.vue'),
        meta: {
          title: '订单',
          icon: 'Bell'
        }
      },
      {
        path: '/store',
        name: 'store',
        component: () => import(/* webpackChunkName: "store" */ '../views/Store.vue'),
        meta: {
          title: '门店',
          icon: 'House'
        }
      },
      {
        path: '/tencentmap',
        name: 'tencentmap',
        component: () => import(/* webpackChunkName: "tencentmap" */ '../views/TencentMap.vue'),
        meta: {
          title: '地图',
          icon:'Location'
        }
      },
      {
        path: '/personal',
        name: 'personal',
        component: () => import(/* webpackChunkName: "personal" */ '../views/Personal.vue'),
        meta: {
          title: '个人中心',
          icon: 'Avatar'
        }
      }
    ]
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//登陆拦截
router.beforeEach((to, from, next) => {
  console.log(to);
  if (to.path != '/login') {
    if (!localStorage.getItem('user')) {
      ElMessage({
        type: 'warning',
        message: '请先登陆！',
      })
      next('/login')
    }
  }
  next()
})


export default router
